package com.hyfd.task;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class WangXinTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	OrderDao orderDao;//订单
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	private static Logger log = Logger.getLogger(WangXinTask.class);
	
//	@Scheduled(fixedDelay = 60000)
	public void queryBoYaOrder(){
		Map<String,Object> map = new HashMap<String,Object>();

		int flag = -1;
		try{
			String id = "0000000087";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);//获取通道的数据
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String queryOrderUrl = paramMap.get("queryOrderUrl")+"";//查询地址
			String key = paramMap.get("key");
			String userName = paramMap.get("userName");
			
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", id);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			for(Map<String,Object> order : orderList){
				String msgId = order.get("orderId")+"";
				map.put("orderId", msgId);
				
				String sign = SHA1("userName" + userName + "msgId" + msgId + "key" + key);
				
				Map<String, String> queryParamMap = new LinkedHashMap<String, String>();
				queryParamMap.put("userName", userName);
				queryParamMap.put("msgId", msgId);
				queryParamMap.put("sign", sign);
				
				String paramData = JSONObject.toJSONString(queryParamMap);
				
				String result = ToolHttp.post(false, queryOrderUrl, paramData, null);
				
				if(result !=null && !result.equals("")){
					JSONObject resultJson = JSONObject.parseObject(result);
					String code = resultJson.getString("code"); // 查询状态
					String msg = resultJson.getString("msg");//充值状态
					if("0000".equals(code)){ //查询成功
						String status = resultJson.getString("status");//充值状态
						String fail_describe = resultJson.getString("fail_describe");//充值状态
						if ("0".equals(status)) { // 订购成功
							flag = 1;
						} else if("45".equals(status)) { // 订购失败
							flag = 0;
						} else if("1".equals(status)){ // 订购中
							continue;
						} else { // 不合法
							log.error("网信查询订单编号[" + msgId + "]的订单成功,充值结果异常,code[" + code + "],原因[" + fail_describe + "]");
							continue;
						}
					}else { //查询失败
						log.error("网信查询订单编号[" + msgId + "]的订单失败,code[" + code + "],原因[" + msg + "]");
						continue;
					}
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		}catch(Exception e){
			log.error("网信流量查询Task出错"+e);
		}
	}
	
	/**
	 * @功能描述：	sha1 加密
	 *
	 * @作者：zhangpj		@创建时间：2017年8月10日
	 * @param decript
	 * @return
	 */
	private String SHA1(String decript) {
		System.out.println("被加密的明文[" + decript + "]");
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
			digest.update(decript.getBytes());
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			// 字节数组转换为 十六进制 数
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();

		} catch (Exception e) {
			e.printStackTrace();
			log.error("乐疯签名方法出错|"+e.getMessage());
		}
		return "";
	}
	
}
